using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace hondaab.portal.articulos_dir
{
    public partial class main_articulos : System.Web.UI.Page
    {
        protected static lineaspedidos lin = new lineaspedidos();
        protected static articulos art = new articulos();
        protected override void OnInit(EventArgs e)
        {
            if (Request["id"] != null)
            {
                base.OnInit(e);
                int idarticulos = sf.entero(Request["id"]);
            }
            else
            {
                Response.Redirect("../market/main_market.aspx");
            }
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lin = new lineaspedidos();
                if (Request["id"] != null)
                {
                    cargarArticulo(sf.entero(Request["id"]));
                  //  lo.comboRellenar(cmbtallas, "select idtallas,nombre from tallasarticulos where idarticulos=" + sf.entero(Request["id"]), ctes.conStringAdoGeneral, "");
                   // cargarColores(sf.entero(Request["id"]));
                    // cargarTallas(sf.entero(Request["id"]));
                }

                //ContentPlaceHolder mpContentPlaceHolder;
                //Button mpTextBox;
                //mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

                //if (mpContentPlaceHolder != null)
                //{
                //    mpTextBox = (Button)mpContentPlaceHolder.FindControl("Button1");
                //    if (mpTextBox != null)
                //    {
                //        mpTextBox.Text = "TextBox found!";
                //    }
                //}
            }
        }

        private void cargarArticulo(int idarticulos)
        {

            art = articulos.getarticulos(sf.entero(idarticulos));
            if (art.idcategoriasarticulos != 0)
            {
                RepeaterOtrosArticulos.DataSource = articulos.getListarticulosByidcategoriasarticulos(art.idcategoriasarticulos, 4, art.idarticulos);

                RepeaterOtrosArticulos.DataBind();
            }



            if (art.novedad)
            {
                litnovedad.Visible = true;
            }

            if (art.oferta)
            {
                litoferta.Visible = true;
            }
            if ( art.stock==0)
            {
                btnaddarticulo.Enabled = false;
                litnostock.Visible = true;
            }

            List<imagenes> listaImagenes = new List<imagenes>();
            listaImagenes = imagenes.getListimagenesByidarticulos(art.idarticulos);
            RepeaterImagenes.DataSource = listaImagenes;
            RepeaterImagenes.DataBind();
            
           // lo.comboRellenar(cmbtallas, "SELECT idtallasarticulos, CONCAT(nombre,' - ' , precio,' EUR') FROM tallasarticulos WHERE idarticulos = " + art.idarticulos, ctes.conStringAdoGeneral, "Seleccione");
            //if (cmbtallas.Items.Count == 2)
            //{
            //    cmbtallas.SelectedIndex = 1;
            //    lblPrecio.Text = tallasarticulos.gettallasarticulos(sf.entero(cmbtallas.SelectedValue)).precio.ToString();

            //}
            
            lo.comboRellenar(cmbcolores, "select idcolores, nombre from coloresarticulos where idarticulos = " + art.idarticulos, ctes.conStringAdoGeneral, "Seleccione");
            if (cmbcolores.Items.Count == 2)
            {
                cmbcolores.SelectedIndex = 1;
            }
            lblNombre.Text = art.nombre;
            lblPrecio.Text = sf.cadena(art.precio);
            Image1.ImageUrl = art.imagen;
            marcas marc = new marcas();
            marc = marcas.getmarcas(art.idmarcas);
            if (marc.imagen != "")
            {
                imbMarca.ImageUrl = marc.imagen;
                litmarca.Text = marc.nombre;
            }
            lblDescripcion.Text = art.descripcion;
            lblCategoria.Text = categoriasarticulos.getcategoriasarticulos(art.idcategoriasarticulos).nombre;
 
        }

        protected void btnaddarticulo_Click(object sender, EventArgs e)
        {
            bool contiene = false;

            lin.cantidad = 1;// sf.entero(txtcantidad.Text);
            lin.idarticulos = sf.entero(Request["id"]);
           // lin.iva = 18;// art.iva;
            lin.nombre = art.nombre;
            tallasarticulos ta = new tallasarticulos();
            ta= tallasarticulos.gettallasarticulos(sf.entero(cmbtallas.SelectedValue));
            lin.precio = ta.precio;
            lin.talla =ta.nombre;
            lin.color = sf.cadena(cmbcolores.SelectedItem.Text);
            List<lineaspedidos> listaCesta = new List<lineaspedidos>();
            if (HttpContext.Current.Session["cesta"] != null)
                listaCesta = (List<lineaspedidos>)HttpContext.Current.Session["cesta"];

            foreach (lineaspedidos line in listaCesta)
            {
                if (line.idarticulos == lin.idarticulos)
                {
                    line.cantidad += 1;
                    contiene = true;
                }
            }
            if (!contiene)
                listaCesta.Add(lin);
            HttpContext.Current.Session["cesta"] = null;
            HttpContext.Current.Session["cesta"] = listaCesta;
            Response.Redirect("../cesta_dir/main_cesta.aspx");


        }

         
 


        public void rfields_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            articulos fieldsx = (articulos)e.Item.DataItem;
        }
        protected void rfields_ItemCreated(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
            {
                articulos ta = new articulos();
                ta = (articulos)e.Item.DataItem;

            }
        }
        protected void rfields_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {
            int id = sf.entero(e.CommandArgument.ToString());
            //lblcat.Text = categoriasgalerias.getcategoriasgalerias(id).categoria;
            //   cargargaleria(id);
        }
        public void rfields2_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            imagenes fieldsx = (imagenes)e.Item.DataItem;

        }
        protected void rfields2_ItemCreated(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
            {
                imagenes ta = new imagenes();
                ta = (imagenes)e.Item.DataItem;
                if (ta != null)
                {
                    //Image1.ImageUrl = ta.imagen;

                    //if (sf.cadena(ta.cat) == "zoom")
                    //{

                    //   ImageButton imb = (ImageButton)e.Item.FindControl("imaboton");
                    //  imb.Width = 500;
                    //   imb = null;
                    //}

                }
            }



        }
        protected void rfields2_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {
            int id = sf.entero(e.CommandArgument.ToString());
            string comando = sf.cadena(e.CommandName.ToString());
            //  cargargaleria(id);
            imagenes ima = new imagenes();

            ima = imagenes.getimagenes(id);

            if (sf.cadena(ima.imagen) != "")
                Image1.ImageUrl = ima.imagen;
            else
            {
                cargarArticulo(sf.entero(Request["id"]));

            }

 
        }


        protected void Image1_Click(object sender, ImageClickEventArgs e)
        {
          //  Response.Redirect("../articulos_dir/galeria_articulos.aspx?id=" + sf.cadena(Request["id"]) + "&idfoto=" + Image1.ImageUrl.ToString());
        }

        protected void cmbtallas_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblPrecio.Text = tallasarticulos.gettallasarticulos(sf.entero(cmbtallas.SelectedValue)).precio.ToString();
        }

        protected void imbMarca_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("../market/main_market.aspx?idmarca=" + art.idmarcas);
        }
       
 

 

    }
}
